## means.r
## Vito D'Orazio
## June 11, 2014
## This script demonstrates three different ways to calculate a vector of means for a given dataset. It introduces functions, sourcing, looping, and the apply functions.  It also demonstrates how R is weakly typed.

# clear environment and set working directory
rm(list=ls())
setwd("/Users/vjdorazio/Desktop/IQSS/privacy_tools/R_workshop")

# load data
load("PUMS5Extract.Rdata")


# functions for calculating the mean
longmean <- function(v) {
    return (sum(v) / length(v))
}

## each of the following functions establishes "means" as an object of different class, but coerces it to a numeric to demonstrate R is weakly typed

# function called mean1 that returns a vector of means for each column of data
mean1 <- function(d) {
    means <- vector(mode="numeric", length=ncol(d))
    cat(paste("function mean1: \n means was a ", class(means), "\n", sep=""))

for(i in 1:ncol(d)) { # for loop
        means[i] <- mean(d[,i])
    }
    cat(means)
    cat(paste("\n means is now a ", class(means), "\n\n"))
}

# function mean2 uses apply to calculate the means for each column
mean2 <- function(d) {
    means <- NULL
    cat(paste("function mean2: \n means was a ", class(means), "\n", sep=""))

    means <- apply(X=d, MARGIN=2, FUN=mean)
    print(means)
    print(paste("means is now a ", class(means), "\n\n"))
}

# function mean3 uses apply and a custom function, longmean, to calculate column means
mean3 <- function(d) {
    means = "hi, i'm means"
    print(paste("function mean3: means was a ", class(means), sep=""))

    means <- apply(X=d, MARGIN=2, FUN=longmean)
    print(means)
    print(paste("means is now a ", class(means)))
}

# call these functions
mean1(mydata)
mean2(mydata)
mean3(d=mydata)

# try source("means.R") in the R console

